package com.seari.c3gallery.service.impl;

import com.seari.c3gallery.bean.DisplayPicBean;
import com.seari.c3gallery.mapper.C3GalleryMapper;
import com.seari.c3gallery.service.C3GalleryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Encoder;

import java.util.Base64;
import java.util.Collections;
import java.util.List;

@Service
public class C3GalleryServiceImpl implements C3GalleryService {

    @Autowired
    C3GalleryMapper c3GalleryMapper;

    @Override
    public List<DisplayPicBean> fetchDisplayPicList() {
        List<DisplayPicBean> displayPicBeans = c3GalleryMapper.fetchDisplayPicList();
        Base64.Encoder encoder = Base64.getEncoder();
        for (DisplayPicBean displayPicBean : displayPicBeans) {
            byte[] picByte = displayPicBean.getPicByte();
            String encode = encoder.encodeToString(picByte);
            displayPicBean.setPic(encode);
        }

        Collections.sort(displayPicBeans); //按升序排序

        return displayPicBeans;
    }
}
